/**
  ******************************************************************************
  * @file    bsp_gpio.c
  * @author  Yun
  * @version V1.0.0
  * @date    2025-02-12
  * @brief   GPIO板级支持包驱动模块实现文件
  * @license EULA (End User License Agreement)
  *          Copyright (c) 2025 星瑜科技自动化 Co., Ltd. All rights reserved.
  ******************************************************************************
  */

#include "bsp_gpio.h"

/**
  * @brief  GPIO硬件初始化
  */
uint8_t Bsp_Gpio_Init(void* pGpioHwCfg)
{
    if(pGpioHwCfg == NULL) return BSP_ERROR;
    Gpio_Hw_Cfg_TypeDef* pstGpio_Hw_Cfg = (Gpio_Hw_Cfg_TypeDef*)pGpioHwCfg;

    /* 使能GPIO端口时钟 */
    if(pstGpio_Hw_Cfg->Gpio_x == GPIOA)       __HAL_RCC_GPIOA_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOB)  __HAL_RCC_GPIOB_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOC)  __HAL_RCC_GPIOC_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOD)  __HAL_RCC_GPIOD_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOE)  __HAL_RCC_GPIOE_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOF)  __HAL_RCC_GPIOF_CLK_ENABLE();
    else if(pstGpio_Hw_Cfg->Gpio_x == GPIOG)  __HAL_RCC_GPIOG_CLK_ENABLE();
    else return BSP_ERROR;

    /* 执行HAL库初始化 */
    HAL_GPIO_Init(pstGpio_Hw_Cfg->Gpio_x, &pstGpio_Hw_Cfg->stGpioInit);

    return BSP_OK;
}

/**
  * @brief  写入GPIO状态
  */
uint8_t Bsp_Gpio_Write(void* pGpioHwCfg, uint8_t u8State)
{
    if(pGpioHwCfg == NULL) return BSP_ERROR;
    Gpio_Hw_Cfg_TypeDef* pstGpio_Hw_Cfg = (Gpio_Hw_Cfg_TypeDef*)pGpioHwCfg;

    HAL_GPIO_WritePin(pstGpio_Hw_Cfg->Gpio_x,
                      pstGpio_Hw_Cfg->stGpioInit.Pin,
                      (GPIO_PinState)u8State);
    return BSP_OK;
}

/**
  * @brief  读取GPIO状态
  */
uint8_t Bsp_Gpio_Read(void* pGpioHwCfg, uint8_t* pu8State)
{
    if(pGpioHwCfg == NULL || pu8State == NULL) return BSP_ERROR;
    Gpio_Hw_Cfg_TypeDef* pstGpio_Hw_Cfg = (Gpio_Hw_Cfg_TypeDef*)pGpioHwCfg;

    *pu8State = (uint8_t)HAL_GPIO_ReadPin(pstGpio_Hw_Cfg->Gpio_x,
                                          pstGpio_Hw_Cfg->stGpioInit.Pin);
    return BSP_OK;
}

/**
  * @brief  GPIO标准接口实例
  */
Gpio_Interface_TypeDef stBspGpioInterface = {
    .Init   = Bsp_Gpio_Init,
    .Write  = Bsp_Gpio_Write,
    .Read   = Bsp_Gpio_Read,
};

/************************ (C) COPYRIGHT 星瑜科技自动化 *****END OF FILE****/
